import { SET_SEARCH_HISTORY } from '../types'

export default {
  state: {
    searchHistory: []
  },
  mutations: {
    [SET_SEARCH_HISTORY](state, history) {
      state.searchHistory = history;
    },
  },
  actions: {
    addSearchHistory({ commit, state }, val) {
      let list = state.searchHistory;
      if (list.indexOf(val) !== -1) {
        list.splice(list.indexOf(val), 1)
        list.unshift(val)
        commit('SET_SEARCH_HISTORY', list)
        return
      }
      if (list.length >= 10) {
        list.splice(9, list.length);
        list.unshift(val)
        commit('SET_SEARCH_HISTORY', list)
        return
      }
      list.unshift(val)
      commit('SET_SEARCH_HISTORY', list)
    },
    clearSearchHistory({ commit }) {
      commit('SET_SEARCH_HISTORY', [])
    }
  }
};